package com.code.zjm.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import com.code.zjm.R;

/**
 * Created by ZJM on 2017/4/27.
 * User 操作界面基类
 */

public abstract class BaseUserActivity extends AppCompatActivity implements ViewSwitcher.ViewFactory, View.OnClickListener {
    //        protected Intent intent = new Intent();//意图，跳转
    protected Intent intent;
    protected int privacy;//用于判断添加的用户是不是保密的
    protected View imageChooseView;//图像选择的视图
    protected AlertDialog imageChooseDialog;//头像选择对话框
    protected Gallery gallery;//头像的Gallery
    protected ImageSwitcher iSwitcher;//头像的ImageSwitcher
    protected int currentImagePosition;//用于记录当前选中图像在图像数组中的位置
    protected int previousImagePosition;//用于记录上一次图片的位置
    protected boolean imageChanged;//判断头像有没有变化
    protected ImageView userImage;//头像
    protected int[] images
            = new int[]{R.mipmap.ic_launcher_round
            , R.drawable.image1, R.drawable.image2, R.drawable.image3
            , R.drawable.image4, R.drawable.image5, R.drawable.image6
            , R.drawable.image7, R.drawable.image8, R.drawable.image9
            , R.drawable.image10, R.drawable.image11, R.drawable.image12
            , R.drawable.image13, R.drawable.image14, R.drawable.image15
            , R.drawable.image16, R.drawable.image17, R.drawable.image18
            , R.drawable.image19, R.drawable.image20, R.drawable.image21
            , R.drawable.image22, R.drawable.image23, R.drawable.image24
            , R.drawable.image25, R.drawable.image26, R.drawable.image27
            , R.drawable.image28, R.drawable.image29, R.drawable.image30};


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(getTAG(), "onCreate: " + this);
        //动态设置视图
        intent = getIntent();
        if (null != getContentView()) {
            //视图不为空时  处理
            if (getContentView() instanceof Integer) {
                setContentView((Integer) getContentView());
            } else if (getContentView() instanceof View) {
                setContentView((View) getContentView());
            }
        } else {
            //视图为空的处理 生成一个空白界面
            View view = new View(this);
            view.setBackgroundColor(getResources().getColor(android.R.color.transparent));
            setContentView(view);
        }

        //设置键盘 与 主界面布局的交互方式
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        //设置 联系人模式
        if (intent != null) {
            if (intent.getExtras() != null && intent.getExtras().getInt("privacy") == 1) {
                privacy = 1;
            } else {
                privacy = 0;
            }
        }

        //ActionBar 设置
        //其中 getSupportActionBar 方法不带参数时获取 ActionBar 对象
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            //图标导航的功能，即在 ActionBar 显示返回图标
            actionBar.setDisplayHomeAsUpEnabled(true);
            //设置 返回图标此处在 style.xml 文件的相关主题中设置了
//            actionBar.setHomeAsUpIndicator(R.drawable.back);
        }

        initData();
        //初始化界面
        initView();
        //初始化监听器
//        initListener();

    }

    /**
     * 获取 TAG
     *
     * @return String
     */
    protected abstract String getTAG();

    /**
     * 获取界面布局
     *
     * @return Object
     */
    protected abstract Object getContentView();

    /**
     * 初始化数据对象
     */
    public void initData() {

    }

    /**
     * 初始化界面
     */
    protected void initView() {
    }
//
//    /**
//     * 初始化监听器
//     */
//    protected void initListener() {
//    }


    /**
     * 为gallery装载图片
     */
    public void loadImage() {
        if (imageChooseView == null) {
            //布局解析对象
            LayoutInflater inflater = LayoutInflater.from(this);
            imageChooseView = inflater.inflate(R.layout.image_switch, null);

            //通过渲染xml文件，得到一个视图（View），再拿到这个View里面的Gallery
            gallery = (Gallery) imageChooseView.findViewById(R.id.gallery);
            //为Gallery装载图片
            gallery.setAdapter(new ImageAdapter(this));
            gallery.setSelection(images.length / 2);
            iSwitcher = (ImageSwitcher) imageChooseView.findViewById(R.id.image_switch);
            iSwitcher.setFactory(BaseUserActivity.this);
            //设置视图的动画效果
            //进入时的效果
            iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
            //退出时的效果
            iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
            gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    //当前的头像位置为选中的位置
                    currentImagePosition = position;
                    //为ImageSwitcher设置图像 无限循环
                    iSwitcher.setImageResource(images[position % images.length]);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    Log.i(getTAG(), "-----------ImageSwitch hint: nothing are selected");
                }
            });
        }

    }

    /**
     * 初始化imageChooseDialog
     */
    public void initImageChooseDialog() {
        if (imageChooseDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("请选择图像")
                    .setView(imageChooseView).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    imageChanged = true;
                    previousImagePosition = currentImagePosition;
                    userImage.setImageResource(images[currentImagePosition % images.length]);
                }
            })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            currentImagePosition = previousImagePosition;

                        }
                    });
            imageChooseDialog = builder.create();
        }
    }


    /**
     * 自定义Gallery的适配器
     *
     * @author Administrator
     */
    class ImageAdapter extends BaseAdapter {

        private Context context;

        ImageAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        /**
         * gallery从这个方法中拿到image
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView iv = new ImageView(context);
            //循环获取
            iv.setImageResource(images[position % images.length]);
            //调整图片边界
            iv.setAdjustViewBounds(true);
            iv.setLayoutParams(new Gallery.LayoutParams(80, 80));
            iv.setPadding(15, 10, 15, 10);
            return iv;

        }

    }


    /**
     * The function of interface {@link android.widget.ViewSwitcher.ViewFactory}.
     * Creates a new {@link android.view.View} to be added in a {@link android.widget.ViewSwitcher}.
     * It is called when setFactory({@link android.widget.ViewSwitcher.ViewFactory}) has been invoked
     *
     * @return {@link android.view.View}
     */
    @Override
    public View makeView() {
        ImageView view = new ImageView(this);
        view.setBackgroundColor(0xff000000);
        view.setScaleType(ImageView.ScaleType.FIT_CENTER);
        view.setLayoutParams(new ImageSwitcher.LayoutParams(90, 90));
        return view;
    }
}
